package com.rabbitmq.config;


import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectRabbitConfig {
/*    //消息确认 添加两个监听类
    @Bean
    RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback( new SendConfirmCallback());//监听类
        rabbitTemplate.setReturnCallback( new SendReturnCallback());//监听类
        return rabbitTemplate;
    }*/
    /**
     * 交换机
     */
    @Bean
    public DirectExchange myDirectExchange() {
        // 参数意义:
        // name: 名称
        // durable: true
        // autoDelete: 自动删除
        return new DirectExchange("myDirectExchange", true, false);
    }
    /**
     * 队列
     */
    @Bean
    public Queue myDirectQueue() {
        return new Queue("myDirectQueue", true);
    }
    /**
     * 队列
     */
    @Bean(name = "myWorkQueues")
    public Queue myWorkQueues() {
        return new Queue("myWorkQueues");
    }
    /**
     * 将队列按照相应的规则绑定到交换机上
     *
     * @param queue    消息队列
     * @param exchange 交换机
     */
    @Bean
    public Binding bindingExchangeMessages(@Qualifier("myWorkQueues") Queue queue,
                                           @Qualifier("myWorkQueues_exchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("myWorkQueues");
    }
    /**
     * 交换机
     */
    @Bean(name = "myWorkQueues_exchange")
    public TopicExchange myWorkQueuesTopicExchange() {
        // 参数意义:
        // name: 名称
        // durable: true
        // autoDelete: 自动删除
        return new TopicExchange("myWorkQueues_exchange");
    }
    /**
     * 绑定
     */
    @Bean
    public Binding bindingDirect() {
        return BindingBuilder.bind(myDirectQueue())
                .to(myDirectExchange())
                .with("my.direct.routing");
    }

    //Fanout交换机测试
    //广播模式
    //1.新建一个交换机测试队列
    @Bean(name = "testExchangesQueues")
    public Queue testExchangesQueues() {

        return new Queue("testExchangesQueues");
        //return n
    }


    //2.新建一个 Fanout交换机
    @Bean(name = "testExchangesQueues_exchange")
    public FanoutExchange testExchangesQueues_exchange() {
        return new FanoutExchange("testExchangesQueues_exchange");
    }
    //3.绑定交换机和队列
    @Bean
    public Binding bindingTestExchangesQueues_exchange() {
        return BindingBuilder.bind(testExchangesQueues())
                .to(testExchangesQueues_exchange());
    }



}
